package com.baiyangtx.lingerweather.view;

import com.baiyangtx.lingerweather.model.WeatherChartModel;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;

public class TemperatureChart extends View {

	
	private WeatherChartModel model ;
	private Paint paint = new Paint();
	
	
	private float hightestTemperature ;
	
	private float lowestTemperature ;
	
	private float paintStrokeWidth = 7 ;
	
	
	private int textSize = 24 ;
	private int textColor = Color.WHITE ;
	
	private boolean isDrawNoon = true ;
	
	
	public TemperatureChart(Context context) {
		super(context);
//		setBackgroundColor(0xE0303030) ;
	}

	
	public void setModel(WeatherChartModel model){
		this.model = model ;
	}
	
	
	

	
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		calculateTemperatureRange() ;
		
		setState(true) ;
		drawTemperatureChart(canvas) ;
		setState(false) ;
		drawTemperatureChart(canvas) ;
//		drawNoonTemperatureChart(canvas) ;
//		drawNightTemperatureChart(canvas) ;
		
	}
	
	
	
	private void drawTemperatureChart(Canvas canvas ){
		final int days = model.getDaysNumber() ;
		final int step = getWidth() / days ;
		
		int start = 0 ;
		int end = days - 1 ;
		int textXOffset = step/3 * (-1) ;
		int textYOffset = getChartOffset()/3  ;
		int lineColor = Color.BLUE ;
		
		if( isDrawNoon() ){
//			start = 1 ;
//			end = days -1 ;
			textYOffset *= -1 ;
			lineColor = Color.RED ;
		}
		
		for( int i = start ; i < end ; i++ ){
			int startX = i*step + step/2 ;
			int startY = getTemperaturePosition( getTemperature(i)) ;
			int endX = startX + step ;
			int endY = getTemperaturePosition( getTemperature(i+1)) ;
			
			paint.setColor(lineColor) ;
			paint.setStrokeWidth(paintStrokeWidth) ;
			paint.setAntiAlias(true) ;
			canvas.drawLine(startX, startY, endX, endY, paint) ;
			
			paint.setColor(textColor);
			paint.setTextSize(textSize) ;
			canvas.drawText(String.format("%d℃", getTemperature(i)), 
					startX + textXOffset , startY + textYOffset , paint) ;
		}
		paint.setColor(textColor);
		paint.setTextSize(textSize) ;
		float lastX = step * end + step/2 + textXOffset ;
		float lastY = getTemperaturePosition( getTemperature(end-1)) + textYOffset ;
		canvas.drawText( String.format("%d℃", getTemperature(end-1)), 
				lastX , lastY , paint) ;
		
	}
	
	
	
	
	private void setState(boolean isDrawNoon ){
		this.isDrawNoon = isDrawNoon ;
	}
	
	private boolean isDrawNoon(){
		return isDrawNoon ;
	}
	
	private int getTemperature(int index ){
		int temperature ;
		if( isDrawNoon ){
			temperature = model.getHighestTemperature(index) ;
		}else{
			temperature = model.getLowestTemperature(index) ;
		}
		return temperature ;
	}
	
	


	
	private void calculateTemperatureRange(){
		hightestTemperature = model.getHighestTemperature(0) ;
		lowestTemperature = hightestTemperature ;
		for( int i = 0 ; i < model.getDaysNumber() ; i++ ){
			float noon = model.getHighestTemperature(i) ;
			float night = model.getLowestTemperature(i) ;
			if( noon < night ){
				float temp = noon ;
				noon = night ;
				night = temp ;
			}
			if( hightestTemperature < noon ){
				hightestTemperature = noon ;
			}
			if( night < lowestTemperature ){
				lowestTemperature = night ;
			}
		}
	}
	
	
	private int getTemperaturePosition( float temperature ){
		
		int chartOffset = getChartOffset() ;
		int chartHeight = chartOffset * 4 ;
		float temperatureRange = hightestTemperature - lowestTemperature ;
		
		int position = chartOffset + (int)( (hightestTemperature - temperature)/temperatureRange*chartHeight ) ;
		return position ;
	}
	
	
	private int getChartOffset(){
		return getHeight() / 6 ;
	}
	
	
	
}
